package com.lottery.im.module.service.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.BaseObserver;
import com.lottery.im.api.result.entity.EmptyData;
import com.lottery.im.api.result.entity.bank.BankList;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.service.presenter.BankSelectPresenter2;
import com.lottery.im.module.service.view.BankSelectView2;
import com.lottery.im.util.ToastUtils;
import com.lottery.im.widget.dialog.LoadingDialogUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

/**
 * 作者：cyy
 * 时间：2018/9/26。
 */
public class BankSelectFragment2 extends BaseSwipeFragment<BankSelectPresenter2>
    implements BankSelectView2 {
  @BindView(R.id.tv_title) TextView tvTitle;
  @BindView(R.id.imageView) ImageView imageView;
  @BindView(R.id.bankName) TextView bankName;
  @BindView(R.id.bandDetail) TextView bandDetail;
  @BindView(R.id.et_bank_name) EditText etBankName;
  @BindView(R.id.et_bank_card) EditText etBankCard;

  private final static int REQUESTCODE=100;
  public final static int RESULTCODE=300;
  private BankList mBankList;

  String token = MyApplication.getInstance().getToken();
  String userId = MyApplication.getInstance().getUserInfo().getUserId();

  @Override protected int getLayoutResId() {
    return R.layout.bank_select2;
  }

  @Override protected void initPresenter() {
    presenter = new BankSelectPresenter2(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    tvTitle.setText("选择银行卡");
  }

  @OnClick({ R.id.iv_back, R.id.container2, R.id.btn_add }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.container2:
        startFragmentForResult(new AddBankFragment(),REQUESTCODE);
        break;
      case R.id.btn_add:
        String realName = etBankName.getText().toString().trim();
        String bankCard = etBankCard.getText().toString().trim();
        if(mBankList==null){
          ToastUtils.showToast(mActivity,"请选择银行卡");
        }else if(TextUtils.isEmpty(realName)||TextUtils.isEmpty(bankCard)){
          ToastUtils.showToast(mActivity,"请输入真实姓名及银行卡号");
        } else{
          ApiClient.getApiRetrofitInstance()
              .userBindCard(userId,token,mBankList.getBankType(),bankCard,realName)
              .throttleFirst(2, TimeUnit.SECONDS)
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new BaseObserver<EmptyData>() {
                @Override public void onStart() {
                  LoadingDialogUtil.showProgressDialog(mActivity);
                }

                @Override public void onSuccess(EmptyData emptyData) {
                  LoadingDialogUtil.cancelProgressDialog();
                  ToastUtils.showToast(mActivity,"绑定成功");
                  setFragmentResult(RESULTCODE,null);
                  popBackStack();
                }

                @Override public void onFail(String code, String msg) {
                  LoadingDialogUtil.cancelProgressDialog();
                  ToastUtils.showToast(mActivity,msg);
                }

                @Override public void onException(String msg) {
                  LoadingDialogUtil.cancelProgressDialog();
                  ToastUtils.showToast(mActivity,msg);

                }
                @Override public void onFinish() {

                }
              });
        }
        break;
    }
  }

  @Override protected void onFragmentResult(int requestCode, int resultCode, Intent data) {
    super.onFragmentResult(requestCode, resultCode, data);
    if(requestCode==REQUESTCODE&&resultCode==AddBankFragment.RESULTCODE){
      mBankList = (BankList) data.getSerializableExtra("banklist");
      bankName.setText(mBankList.getBankName());
      bandDetail.setText(mBankList.getBankType());
      Glide.with(mActivity)
          .load("file:///android_asset/bank/"+"bank_"+mBankList.getBankType().toLowerCase()+".png")
          .apply(new RequestOptions()
              .placeholder(R.drawable.add)
              .error(R.drawable.add)
              .fitCenter())
          .into(imageView);
    }
  }
}
